package com.itheima.tanhua.server.controller;

import com.itheima.commons.holder.TokenHolderUserId;
import com.itheima.tanhua.model.dto.LocationDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author H Monster
 */
@RestController
@RequestMapping("/baidu")
public class BaiduController {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/location")
    public ResponseEntity submitLocation(@RequestBody LocationDto dto) {

        String userId = StringUtils.isNotBlank(dto.getUserId()) ? dto.getUserId() : TokenHolderUserId.get();

        Double longitude = dto.getLongitude();
        Double latitude = dto.getLatitude();

        String addrStr = dto.getAddrStr();

        // 将经纬度信息，和当前位置名称写入Redis
        // key为当前用户的id
        Point point = new Point(longitude, latitude);

        // geo参数代表数据存储的分组名
        // add(参数一：坐标，值）
        redisTemplate.boundGeoOps("geo").add(point, userId);

        return ResponseEntity.ok(null);
    }
}